package com.x.provider.customer.factory.dto.impl;

import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerVerificationDTO;
import com.x.provider.customer.configure.CustomerApplicationConfig;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.model.query.customer.CustomerVerificationQuery;
import com.x.provider.customer.service.customer.CustomerVerificationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service("customerVerificationBuilder")
public class CustomerVerificationBuilderImpl implements CustomerDTOBuilder {

   private final CustomerVerificationService verificationService;

   public CustomerVerificationBuilderImpl(CustomerVerificationService verificationService,
                                          CustomerApplicationConfig applicationConfig){
      this.verificationService = verificationService;
   }

   @Override
   public void build(CustomerListReqDTO listCustomerAO, Map<Long, CustomerDTO> dest) {
      if (!listCustomerAO.getCustomerOptions().contains(CustomerOptions.VERIFICATION.name())){
         return;
      }
      List<CustomerVerificationDTO> verificationList = BeanUtil.prepare(verificationService.list(CustomerVerificationQuery.builder()
              .customerIdList(listCustomerAO.getCustomerIds()).build()), CustomerVerificationDTO.class);
      Map<Long, List<CustomerVerificationDTO>> customerResumeMap = verificationList.stream().collect(Collectors.groupingBy(item -> item.getCustomerId()));
      dest.entrySet().forEach(item -> {
         List<CustomerVerificationDTO> customerResume = customerResumeMap.get(item.getKey());
         item.getValue().setVerificationList(CollectionUtils.isEmpty(customerResume) ? new ArrayList<>() : customerResume);
      });
   }
}
